//
//  UIButton+THEventInterval.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 BaseBusiness. All rights reserved.
//

#import "UIButton+THEventInterval.h"
#import "THDeBugTool.h"
@interface UIButton ()
@property (nonatomic,retain) NSNumber *hqEnable;

@end

@implementation UIButton (THEventInterval)

+ (void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        ExchangeImplementationsInTwoClasses([UIButton class],
                                            @selector(sendAction:to:forEvent:),
                                            [UIButton class],
                                            @selector(zh_sendAction:to:forEvent:));
    });
}
- (void)zh_sendAction:(SEL)action to:(nullable id)target forEvent:(nullable UIEvent *)event{
    if ([self isMemberOfClass:[UIButton class]]) {
        if (self.hqEventInterval > 0) {
            if (self.hqEnable.boolValue == YES) {
                self.hqEnable = @(0);
                [self zh_sendAction:action to:target forEvent:event];
                [self performSelector:@selector(setHqEnable:) withObject:@(1) afterDelay:self.hqEventInterval];
            }
        }else{
             [self zh_sendAction:action to:target forEvent:event];
        }
    }else{
        [self zh_sendAction:action to:target forEvent:event];
    }
}
- (void)setHqEventInterval:(NSUInteger)hqEventInterval{
    objc_setAssociatedObject(self,"huanque_hqEventInterval", @(hqEventInterval),OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSUInteger)hqEventInterval{
    NSNumber *hqEventInterval = objc_getAssociatedObject(self,"huanque_hqEventInterval");
    return hqEventInterval ? hqEventInterval.unsignedIntegerValue : 0;
}
- (void)setHqEnable:(NSNumber*)hqEnable{
    objc_setAssociatedObject(self,"huanque_hqEnable",hqEnable, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSNumber*)hqEnable{
    NSNumber *hqEnable = objc_getAssociatedObject(self,"huanque_hqEnable");
    return hqEnable ? hqEnable : @(1);
}
@end
